package com.think.teaching.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.think.teaching.model.Class;
import com.think.teaching.model.Major;
import com.think.teaching.model.StudentInfo;

import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

/**
 * <p>
 * 专业 Mapper 接口
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
public interface MajorMapper extends BaseMapper<Major> {
	
	@Select("SELECT * FROM major WHERE id = #{major}")
	@Result(column="college",property="collegeData",
	one=@One(select="com.think.teaching.mapper.CollegeMapper.getInfoById",fetchType= FetchType.EAGER))
	Major getInfoById(Integer major);
	
	@Select("<script>" +
			"SELECT * FROM major WHERE delflag =0 " +
			"<if test='state gt 0'> AND state =#{state}</if>"
			+ "AND major.college IN(SELECT college.id FROM college,school WHERE  school.id=college.school"
			+ "<if test='college gt 0'> AND college.id =#{college}</if>"
			+ "<if test='school gt 0'> AND school.id =#{school}</if>)"
			+
			"</script>"
	)
	@Results({
		@Result(column="id",property="id"),
		@Result(column="major_name",property="majorName"),
		@Result(column="state",property="state"),
		@Result(column="updated_time",property="updatedTime"),
		@Result(column="college",property="collegeData",
		one=@One(select="com.think.teaching.mapper.CollegeMapper.getInfoById",fetchType= FetchType.EAGER))
	})
	IPage<Major> getMajorList(IPage<Major> page,
							  @Param("college") Integer college,
							  @Param("school") Integer school,
							  @Param("state")  Integer state);
	
	@Select("SELECT * FROM class WHERE delflag = 0 AND state=1 AND major=#{id}")
	List<Class> getMajorClasses(Integer id);
	
	@Select("SELECT * FROM student_info WHERE delflag = 0 AND state=1 AND class_id IN(SELECT class.id FROM class WHERE delflag = 0 AND state=1 AND major=#{id})")
	List<StudentInfo> getMajorStudents(Integer id);
}
